/*
 * LogIn.java
 *
 * Created on April 2, 2007, 11:13 AM
 */

package edu.byu.isys413.jjenkins.view;

import edu.byu.isys413.jjenkins.data.*;
import java.util.List;
import javax.swing.JOptionPane;

/**
 * The form is generated in order for customer to put in their login credentials and login
 * @author This Login allows the users to put in their user name and password to initiate a process. Without a login the individual cannot access the
 * the system.
 */
public class LogIn extends javax.swing.JFrame {
    
    List<EmployeeBO> employees;
    List<StoreBO> stores;
    
    
    /** Creates new form LogIn */
    public LogIn() {
        initComponents();
        populateEmployee();
        populateStore();
        this.setLocationRelativeTo(null);
        bannerPanel.setPreferredSize(new java.awt.Dimension(800, 75));
        JLabelPic.setIcon(new javax.swing.ImageIcon(getClass().getResource("logoWeb5.gif")));
        bannerPanel.add(JLabelPic);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        bannerPanel = new javax.swing.JPanel();
        JLabelPic = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        cmbEmployee = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        cmbStore = new javax.swing.JComboBox();
        btnLogin = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Login");
        bannerPanel.add(JLabelPic);

        getContentPane().add(bannerPanel, java.awt.BorderLayout.NORTH);

        jPanel2.setPreferredSize(new java.awt.Dimension(500, 250));
        jLabel3.setText("M.I.S.S. Login");
        jLabel3.setBorder(javax.swing.BorderFactory.createMatteBorder(0, 0, 1, 0, new java.awt.Color(0, 0, 0)));
        jLabel3.setPreferredSize(new java.awt.Dimension(275, 14));
        jPanel2.add(jLabel3);

        jPanel1.setLayout(new java.awt.GridLayout(3, 2, 10, 10));

        jPanel1.setBorder(javax.swing.BorderFactory.createEmptyBorder(20, 20, 20, 20));
        jLabel1.setText("Employee:");
        jPanel1.add(jLabel1);

        cmbEmployee.setPreferredSize(new java.awt.Dimension(120, 20));
        jPanel1.add(cmbEmployee);

        jLabel2.setText("Store:");
        jPanel1.add(jLabel2);

        cmbStore.setPreferredSize(new java.awt.Dimension(50, 20));
        jPanel1.add(cmbStore);

        btnLogin.setText("Login");
        btnLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoginActionPerformed(evt);
            }
        });

        jPanel1.add(btnLogin);

        jPanel2.add(jPanel1);

        getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoginActionPerformed
        try{
        CurrentOptsBO opts = CurrentOptsDAO.getInstance().create();
        opts.SignIn(employees.get(cmbEmployee.getSelectedIndex()).getId(), stores.get(cmbStore.getSelectedIndex()).getId());
        System.out.println("current opts" +opts);
        // We put currentopts in cache so that if it is inactive for too long, the user must log back in
        Cache.getInstance().put("currentOpts", opts);
        SelectCustomer view = SelectCustomer.getInstance();
        view.setVisible(true);
        view.setLocationRelativeTo(null);
             
        
        }catch(DataException ex){
            JOptionPane.showMessageDialog(null,"Cannot Log In and Create Current Operations Variable","Error Creating New Order", JOptionPane.ERROR_MESSAGE);
            ex.printStackTrace();
        }
    }//GEN-LAST:event_btnLoginActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                
                new LogIn().setVisible(true);
                
            }
        });
        
    }
    
    /**
     *populates employee in the view
     */
    public void populateEmployee(){
        try{
            employees = EmployeeDAO.getInstance().readAll();
            for(int i = 0; i < employees.size(); i++){
                cmbEmployee.addItem(employees.get(i).getName());
                
                
            }
        }catch(Exception ex){
            JOptionPane.showMessageDialog(null,ex.toString(),"Cannot get employees", JOptionPane.ERROR_MESSAGE);
            
        }
        
    }
    
    /**
     *populates the store in the view
     */
    public void populateStore(){
        try{
            stores = StoreDAO.getInstance().readAll();
            for(int i = 0; i < stores.size(); i++){
                cmbStore.addItem(stores.get(i).getLocation());
            }
        }catch(Exception ex){
            JOptionPane.showMessageDialog(null,ex.toString(),"Cannot get employees", JOptionPane.ERROR_MESSAGE);
            
        }
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel JLabelPic;
    private javax.swing.JPanel bannerPanel;
    private javax.swing.JButton btnLogin;
    private javax.swing.JComboBox cmbEmployee;
    private javax.swing.JComboBox cmbStore;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    // End of variables declaration//GEN-END:variables
    
}
